//
//  SPHomeViewController.m
//  Sport
//
//  Created by Cass on 2017/11/1.
//  Copyright © 2017年 Cass. All rights reserved.
//

#import "SPHomeViewController.h"
#import "UIBarButtonItem+SPExtension.h"
#import "SPAddFriendsViewController.h"
@interface SPHomeViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSTimer *timer;
@end

@implementation SPHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.navigationItem.title = @"首页";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"btn_xuanze_1" selectedImage:@"" title:@"定位中" target:self action:@selector(leftItemTapped:)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"tab_icon_jia" selectedImage:@"tab_icon_jia" title:@"" target:self action:@selector(rightItemTapped:)];

    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.scrollView.mas_centerX);
        make.bottom.mas_equalTo(self.scrollView).with.offset(0);
    }];
    
    [self setScrollImg];
    [self addTimer];
    
}

- (void)leftItemTapped:(UIBarButtonItem *)item{
    SPCityViewController *cityVC = [[UIStoryboard storyboardWithName:@"Home" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPCityViewController class])];
    cityVC.title = @"切换城市";
    [self.navigationController pushViewController:cityVC animated:YES];
    
}

- (void)rightItemTapped:(UIBarButtonItem *)ietm{
    SPAddFriendsViewController *addVC = [[UIStoryboard storyboardWithName:@"Message" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPAddFriendsViewController class])];
    [self.navigationController pushViewController:addVC animated:YES];
}
- (void)setScrollImg{
    CGFloat imgWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat imgHeight = 150.0;
    for (int i = 0; i < 3; i++) {
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(i*imgWidth, 0, imgWidth, imgHeight)];
        NSString *imgName = [NSString stringWithFormat:@"img_0%d",i+1];
        imgView.image = [UIImage imageNamed:imgName];
        [self.scrollView addSubview:imgView];
    }
    self.scrollView.contentSize = CGSizeMake(3*imgWidth, 0);
}

#pragma mark - UITableViewDataSource -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = @"biu";
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark - UITableViewDelegate -

#pragma mark - UIScrollViewDelegate -
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetX = self.scrollView.contentOffset.x;
    int pageNumber = (offsetX + scrollView.frame.size.width*0.5)/scrollView.frame.size.width;
    self.pageControl.currentPage = pageNumber;
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //销毁计时器
    [self.timer invalidate];
    self.timer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addTimer];
}
#pragma mark - 添加计时器 -
- (void)addTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(changeImg) userInfo:nil repeats:YES];
    //获取当前循环对象
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    //改变self.Timer的优先级
    [runloop addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)changeImg{
    NSInteger page = self.pageControl.currentPage;
    if (page == self.pageControl.numberOfPages-1) {
        page = 0;
    }else{
        page++;
    }
    CGFloat offsetX = self.scrollView.frame.size.width*page;
    [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

@end
